<?php
declare (strict_types = 1);

namespace app\api\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserMerchantRecharge extends Model
{
    //
    protected $autoWriteTimestamp = true;
    /**
     * 变更用户在某个商户下的充值记录
     * @param $order
     * @return UserMerchantRecharge|bool|int|Model
     */
    public static function changeRecord($order){
        $where = ['user_id'=>$order['user_id'],'manager_id'=>$order['manager_id']];
        $data = self::where($where)->findOrEmpty();
        $price = $order['price'] + $order['send_price'];
        $result = false;
        if(!empty($data->toArray())){
            $result = $data->where($where)->inc('price', $price)->update();
        }else{
            $result = self::create(
                [
                    'user_id' => $order['user_id'],
                    'manager_id' => $order['manager_id'],
                    'price' => $price,
                ]
            );
        }
        return $result;
    }
}
